package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.jiuzhou.JiuZhouUserBaseData;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.server.gameplayer.module.activity.jiuzhou.JiuZhouModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.List;

public class JiuZhouRmbMallLimit extends BaseActivityMallLimit {

    public JiuZhouRmbMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        if (activityInfo.getType() == eActivityType.JiuZhou.getValue()) {
            JiuZhouConfig config = JiuZhouMgr.getConfig();
            if (config == null) {
                return GameErrorCode.E_ACTIVITY_NO_FOUND;
            }
        }
        if (!ActivityMgr.activityInTime(activityInfo)) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        return 0;
    }

    @Override
    protected boolean isSystemUnLock() {
        if (activityInfo.getType() == eActivityType.JiuZhou.getValue()) {
            return SystemOpenMgr.systemOpen(player, eSystemId.JiuZhou.getValue());
        }
        return false;
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        return activityMallCountLimit(player);
    }

    @Override
    protected int logicCheck() {
        // 判断下可否购买
        // 判断下九州的资格
        JiuZhouUserBaseData userData = player.getModule(JiuZhouModule.class).getUserData();
        if (userData == null) {
            return GameErrorCode.E_MALL_LIMIT_BUY;
        }
        // 只有在开战期内可以买
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            return GameErrorCode.E_MALL_NO_ACTIVITY_ABLE;
        }
        List<Integer> jiuZhouTime = config.getJiuZhouTime();
        // 报名期结束
        int startSignTime = jiuZhouTime.get(1);

        long startTimeStamp = JiuZhouMgr.getTimeStampAfterBegin(startSignTime);

        long now = System.currentTimeMillis();
        if (now < startTimeStamp){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if (now > activityInfo.getEndTime() * 1000) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        // 判断下有没有资格
        if (userData.getHasSignUp() != 1 || userData.getZoneType() == 0) {
            return GameErrorCode.E_MALL_NO_ACTIVITY_ABLE;
        }
        return 0;
    }
}